#! /usr/bin/env python

from bs4 import BeautifulSoup
import re
import sys
import os

filename=sys.argv[1]

soup = BeautifulSoup(open(filename))

tag = soup.find('td',{ 'class': 'champion_render'})
m = re.search("/(\d+).jpg",tag.img.attrs['src'])
#print "[%s]" % (m.group(1))

champid=m.group(1)

tag = soup.find('h2')
temp = []
for i in tag.find_all('span'):
    temp.append(i.string)

#print "name =",temp[0]
#print "title =",temp[1]

champname = temp[0]
champtitle = temp[1]

print "class Champ_%s:" % (champid)
print "    def __init__(self):"
print "        self.ID=%s" % (champid)
print "        self.Name=\"%s\"" % (champname)
print "        self.Title=\"%s\"" % (champtitle)
print "        self.Icon=\"%s.jpg\"" % (champid)
print "        self.Level=1"
print "        self.Build={'1':False,'2':False,'3':False,'4':False,'5':False,'6':False}"

for tag in soup.find_all(True):
    if tag.name == "table":
        if tag['class'][0] == "stats_table":
            for tr in tag.find_all('tr'):
                msg=""
                for td in tr.find_all('td'):
                    if td['class'][0] == "stats_modifier":
                        if td.span == None:
                            pass
                        else:
                            m = re.search('\+([^ ]+)\s+',str(td.span))
                            if len(m.group()) == 0:
                                print "oasch"
                            else:
                                #print m.group(0
                                msg +=str(m.group(1)) +","
                    else:
                        msg +=str(td.string) + ","
                chunk = msg.split(',')
                chunk[0]=re.sub(' ','_',chunk[0])

                if len(chunk) == 3:
                    if chunk[0] == "Move_Speed":
                        chunk[0] = "Movement_Speed"
                    if chunk[0] == "Spell_Block":
                        chunk[0] = "Magic_Resist"

                    print "        self.Base%s=%s" % (chunk[0],chunk[1])
                    print "        self.%s=0" % (chunk[0])
                else:
                    if chunk[0] == "Health_Regen" or chunk[0] == "Mana_Regen":
                        print "        self.Base%s_per_5_seconds=%s\n        self.Base%s_delta = %s\n        self.%s_per_5_seconds=0" % (chunk[0],chunk[1],chunk[0],chunk[2],chunk[0])
                    elif chunk[0] == "Damage":
                        print "        self.BaseAttack_Damage=%s\n        self.BaseAttack_Damage_delta=%s\n        self.Attack_Damage=0.0" % (chunk[1],chunk[2])
                    elif chunk[0] == "Spell_Block":
                        chunk[0] = "Magic_Resist"
                        print "        self.Base%s=%s\n        self.Base%s_delta = %s\n        self.%s=0" % (chunk[0],chunk[1],chunk[0],chunk[2],chunk[0])
                    else:
                        print "        self.Base%s=%s\n        self.Base%s_delta = %s\n        self.%s=0" % (chunk[0],chunk[1],chunk[0],chunk[2],chunk[0])


print "        self.BaseAbility_Power=0"
print "        self.Ability_Power=0"
print "        self.BaseSpell_Vamp=0"
print "        self.Spell_Vamp=0"
print "        self.BaseAttack_Speed=0"
print "        self.Attack_Speed=0"
print "        self.BaseLife_Steal=0"
print "        self.Life_Steal=0"
print "        self.BaseCooldown_Reduction=0"
print "        self.Cooldown_Reduction=0"
print """        self.BaseCritical_Strike_Chance=0
        self.Critical_Strike_Chance=0
        self.BaseMagic_Penetration=0
        self.Magic_Penetration=0
        self.BaseArmor_Penetration=0
        self.Armor_Penetration=0
        # Percentage Bonuses
        self.Movement_Speed_Percentage=0
        self.Attack_Damage_Percentage=0
        self.Mana_Percentage=0
        self.Ability_Power_Percentage=0
        self.Attack_Speed_Percentage=0
        self.Health_Percentage=0
        self.Tenacity=False
        """

print "        # Skills"

skills = ['Skill_Q','Skill_W','Skill_E','Skill_R','Skill_P']
count = 0
tag = soup.find('table',{'class':'abilities_table'})
#print tag.prettify()
count_skill=1
for tr in tag.find_all('tr'):
#    icon = tr.td.img['src']
#    print "    def %s(self,level):" % (skills[count])

    td = tr.find('td',{'class':'ability_detail'})
    print "        self.%s_Name=\"%s\"" % (skills[count],td.span.string)
    print "        self.%s_Icon=\"%s-%s.jpg\"" % (skills[count],champid,count_skill)
    count_skill += 1
    print "        self.%s_MaxStack=False" % (skills[count])
    print "        self.%s_Stack=0" % (skills[count])
    print "        self.%s_Level=False" % (skills[count])
    print "        self.%s_has_Passive=False" % (skills[count])
    print "        self.%s_Cooldown=[]" % (skills[count])
    print "        self.%s_Cost=[]" % (skills[count])
    print "        self.%s_Priority=[]" % (skills[count])
    count += 1
#    print ""

print "        self.Custom()"
print "        self.CalcStats()"

print """
    def ResetStats(self):
        self.Custom()
        self.Attack_Damage=self.BaseAttack_Damage + self.BaseAttack_Damage_delta * self.Level
        self.Health=self.BaseHealth + self.BaseHealth_delta * self.Level
        self.Mana=self.BaseMana + self.BaseMana_delta * self.Level
        self.Health=self.BaseHealth + self.BaseHealth_delta * self.Level
        self.Movement_Speed=self.BaseMovement_Speed
        self.Armor=self.BaseArmor + self.BaseArmor_delta * self.Level
        self.Magic_Resist=self.BaseMagic_Resist + self.BaseMagic_Resist_delta * self.Level
        self.Health_Regen_per_5_seconds=self.BaseHealth_Regen_per_5_seconds + self.BaseHealth_Regen_delta * self.Level
        self.Mana_Regen_per_5_seconds=self.BaseMana_Regen_per_5_seconds + self.BaseMana_Regen_delta * self.Level
        self.Ability_Power=0
        self.Spell_Vamp=0
        self.Attack_Speed=0
        self.Life_steal=0
        self.Cooldown_Reduction=0
        self.Armor_Penetration=0
        self.Critical_Strike_Chance=0
        self.Magic_Penetration=0

    def SetChampLevel(self,level):
        self.Level=level
        self.CalcStats()
        
    def DumpStats(self):
        print "Attack_Damage ", self.Attack_Damage
        print "Health ", self.Health
        print "Mana ", self.Mana
        print "Movement_Speed " ,self.Movement_Speed
        print "Armor ", self.Armor
        print "Magic_Resist ", self.Magic_Resist
        print "Health_Regen ", self.Health_Regen_per_5_seconds
        print "Mana_Regen ", self.Mana_Regen_per_5_seconds
        print "Ability_Power ", self.Ability_Power
        print "Spell_Vamp ", self.Spell_Vamp
        print "Attack_Speed ", self.Attack_Speed
        print "Life_Steal ", self.Life_steal
        print "Cooldown_Reduction ", self.Cooldown_Reduction
        print "Armor_Penetration ", self.Armor_Penetration
        print "Magic_Penetration ", self.Magic_Penetration
        print "Critical_Strike_Chance ", self.Critical_Strike_Chance
        print "Move_Speed_Percentage ", self.Movement_Speed_Percentage
        print "Attack_Speed_Percentage ", self.Attack_Speed_Percentage
        print "-------- Skills -------"
        self.Skill_Q(True)
        self.Skill_W(True)
        self.Skill_E(True)
        self.Skill_R(True)
        
    
    def CalcStats(self):
        # Reset stats
        self.ResetStats()
        # Add Runes
        #self.ProcessRunes()
        # Add Masteries
        #self.ProcessMasteries()

        # loop through skills passives
        self.Skill_Q(False)
        self.Skill_W(False)
        self.Skill_E(False)
        self.Skill_R(False)
        self.Skill_P()
        # loop through items
        for item in self.Build.itervalues():
            if item:
                item.UpdateStats()
        self.CustomCalcStats()
        # Percentage Bonuses

        self.Movement_Speed = self.Movement_Speed * (1+self.Movement_Speed_Percentage/100)
        self.Attack_Damage = self.Attack_Damage * (1+self.Attack_Damage_Percentage/100)
        self.Attack_Speed = self.Attack_Speed * (1+self.Attack_Speed_Percentage/100)
        self.Mana = self.Mana * (1+self.Mana_Percentage/100)
        self.Ability_Power = self.Ability_Power * (1+self.Ability_Power_Percentage/100)
        self.Health = self.Health * (1+self.Health_Percentage/100)

        self.Power_Percentage=0
        self.Health_Percentage=0


    def AddItem(self,pos,itemid):
        self.Build[pos]=ITEMS.GetItem(itemid)
        self.Build[pos].parent=self
        self.CalcStats()

    def DelItem(self,pos):
        self.Build[pos]=False
        self.CalcStats()
"""
if os.path.isfile("Custom/%s.py" % (champid)):
        FILE=open("Custom/%s.py" % (champid))
        for i in FILE.readlines():
            print i.rstrip()
        FILE.close()
else:
        print """
    def Custom(self):
        pass

    def CustomCalcStats(self):
        pass

    def Skill_Q(self,active):
        pass
        
    def Skill_W(self,active):
        pass
        
    def Skill_E(self,active):
        pass
        
    def Skill_R(self,active):
        pass

    def Skill_P(self):
        pass
"""
#os.system("echo '%s' >> champlist.txt" % (champid))
